﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordFun.Models
{
    /// <summary>
    /// class for an English word
    /// </summary>
    public class ModelWord : IWin8UI
    {
        private static Point2D DEFAULT_LOCATION = new Point2D(0, 0); //Location of the first char on the screen
        private static int DEFAULT_DISTANCE = 150; //default distance of two char on the screen

        private List<ModelChar> _listChars; // a list of chars

        /// <summary>
        /// Default constructor
        /// </summary>
        public ModelWord() 
        {
            Initialize(""); // create a empty word
        }

        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="inputWord">Word for game</param>
        public ModelWord(string inputWord) 
        {
            Initialize(inputWord); // create a word contain inputWord
        }

        /// <summary>
        /// Check to modify the word
        /// </summary>
        public void CheckModify() 
        {
            CheckSwap();    
        }

        /// <summary>
        /// Init word
        /// </summary>
        /// <param name="inputString"></param>
        private void Initialize(string inputString)
        {
            _listChars = new List<ModelChar>(); //create new list of ModelChar

            foreach (char charContent in inputString) //check each char in inputString
            {
                _listChars.Add(CreateNewModelChar(charContent)); //add new ModelChar to list
            }
        }

        /// <summary>
        /// Create new ModelChar for Word
        /// </summary>
        /// <param name="charContent">a char</param>
        private ModelChar CreateNewModelChar(char charContent)
        {
            var location = new Point2D(DEFAULT_LOCATION.X + DEFAULT_DISTANCE * _listChars.Count , DEFAULT_LOCATION.Y);// create location of char on the screen
            return new ModelChar(charContent, ref location);
        }

        /// <summary>
        /// Check to swap two ModelChar
        /// </summary>
        private void CheckSwap()
        {
            List<int> selectedIDs = new List<int>(); //selected id list

            for (int i = 0; i < _listChars.Count; i++)
            {
                if (_listChars[i].GetCurrentState() == Utils.ControlCharStates.Selecting) //if char is selecting save current id
                {
                    selectedIDs.Add(i);
                }
            }

            //if two char is selecting, swap both of them
            if (selectedIDs.Count == 2)
            {
                var item = _listChars[selectedIDs[1]];
                _listChars[selectedIDs[0]].Swap(ref item);
            }
        }

        /// <summary>
        /// Implement IWin8UI
        /// </summary>
        /// <param name="collection"></param>
        public void SetUI(Windows.UI.Xaml.Controls.UIElementCollection collection)
        {
            foreach (var item in _listChars) //add all control win8 to collection
            {
                collection.Add(item.Control);
            }
        }
    }
}
